package com.jjlin.chapter_8;

import java.util.*;
public class CD151{

    public static void printMatrixZigZig(int[][] matrix){
        if(matrix == null || matrix[0].length == 0)
            return;
        int tR = 0, tC = 0;
        int dR = 0, dC = 0;
        int endR = matrix.length - 1;
        int endC = matrix[0].length - 1;
        boolean fromUp = false;
        while(tR <= endR){
            printLevel(matrix, tR, tC, dR, dC, fromUp);
            tR = tC == endC ? tR + 1 : tR;
            tC = tC == endC ? tC : tC + 1;
            //下面这两行的顺序不能调换，因此先更新dR会影响的dC的更新，上面两行同理
            dC = dR == endR ? dC + 1 : dC;
            dR = dR == endR ? dR : dR + 1;
            fromUp = !fromUp;
        }
    }

    public static void printLevel(int[][] matrix, int tR, int tC, int dR, int dC, boolean fromUp){
        if(fromUp){
            while(tR != dR + 1)
                System.out.print(matrix[tR++][tC--] + " ");
        }else{
            while(tR != dR + 1)
                System.out.print(matrix[dR--][dC++] + " ");
        }
    }

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int row = sc.nextInt();
        int col = sc.nextInt();
        int[][] matrix = new int[row][col];
        for(int i = 0; i < row; i++)
            for(int j = 0; j < col; j++)
                matrix[i][j] = sc.nextInt();
        printMatrixZigZig(matrix);
    }
}


